using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WozLib.UI.Ink.SketchProperties;

namespace WozLib.UI.Controls
{
  /// <summary>
  /// Interaction logic for OpacitySelector.xaml
  /// </summary>

  public partial class OpacitySelector : UserControl
  {
    public static readonly DependencyProperty SelectedOpacityProperty =
            DependencyProperty.Register("SelectedOpacity", typeof(double), typeof(OpacitySelector),
      new UIPropertyMetadata(0.99999, new PropertyChangedCallback(SelectedOpacity_Changed)));

    public double SelectedOpacity
    {
      get { return (double)GetValue(SelectedOpacityProperty); }
      set { SetValue(SelectedOpacityProperty, value); }
    }

    public static readonly DependencyProperty AllowZeroProperty =
            DependencyProperty.Register("AllowZero", typeof(bool), typeof(OpacitySelector),
      new UIPropertyMetadata(true, new PropertyChangedCallback(AllowZeroProperty_Changed)));

    public bool AllowZero
    {
      get { return (bool)GetValue(AllowZeroProperty); }
      set { SetValue(AllowZeroProperty, value); }
    }

    public static readonly DependencyProperty CascadingPropertyProperty =
            DependencyProperty.Register("CascadingProperty", typeof(CascadingProperty), typeof(OpacitySelector));

    public CascadingProperty CascadingProperty
    {
      get { return (CascadingProperty)GetValue(CascadingPropertyProperty); }
      set { SetValue(CascadingPropertyProperty, value); }
    }


    public OpacitySelector()
    {
      InitializeComponent();
      this.Loaded += new RoutedEventHandler(OpacitySelector_Loaded);
    }

    void OpacitySelector_Loaded(object sender, RoutedEventArgs e)
    {
      _ZeroRadio.Checked += new RoutedEventHandler(_ZeroRadio_Checked);
      _OneRadio.Checked += new RoutedEventHandler(_OneRadio_Checked);
      _TwoRadio.Checked += new RoutedEventHandler(_TwoRadio_Checked);
      _FiveRadio.Checked += new RoutedEventHandler(_FiveRadio_Checked);

      _SmallSlider.PreviewMouseDown += new MouseButtonEventHandler(_Slider_PreviewMouseDown);

      _SmallSlider.PreviewMouseUp += new MouseButtonEventHandler(_Slider_PreviewMouseUp);
    }

    void _Slider_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
      if (CascadingProperty != null)
      {
        CascadingProperty.InContinuousChange = true;
      }
    }

    void _Slider_PreviewMouseUp(object sender, MouseButtonEventArgs e)
    {
      if (CascadingProperty != null)
      {
        CascadingProperty.InContinuousChange = false;
      }
    }

    void _FiveRadio_Checked(object sender, RoutedEventArgs e)
    {
      if (!_suspendSet)
      {
        this.SelectedOpacity = 1.0;
      }
    }

    void _TwoRadio_Checked(object sender, RoutedEventArgs e)
    {
      if (!_suspendSet)
      {
        this.SelectedOpacity = 0.5;
      }
    }

    void _OneRadio_Checked(object sender, RoutedEventArgs e)
    {
      if (!_suspendSet)
      {
        this.SelectedOpacity = 0.2;
      }
    }

    void _ZeroRadio_Checked(object sender, RoutedEventArgs e)
    {
      if (!_suspendSet)
      {
        this.SelectedOpacity = 0.0;
      }
    }

    private static void AllowZeroProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
    { }

    private bool _suspendSet = false;
    //private static void SelectedOpacity_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
    //{

    //  if (d is OpacitySelector && e.NewValue is double)
    //  {
    //    OpacitySelector OpacitySelector = d as OpacitySelector;
    //    double opacity = (double)e.NewValue;
    //    if (!OpacitySelector._suspendSet)
    //    {
    //      // assume uniform Opacity
    //      OpacitySelector.SelectedOpacity = opacity;
    //    }
    //  }
    //}
    private static void SelectedOpacity_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {

      if (d is OpacitySelector && e.NewValue is double)
      {
        OpacitySelector OpacitySelector = d as OpacitySelector;
        double Opacity = (double)e.NewValue;
        if (!OpacitySelector._suspendSet)
        {
          OpacitySelector._suspendSet = true;

          OpacitySelector.SelectedOpacity = Opacity;

          // Zero
          if (Opacity < 0.05 && OpacitySelector.AllowZero)
          {
            OpacitySelector._ZeroRadio.IsChecked = true;
          }
          // One
          else if (Opacity < 0.25 && Opacity > 0.15)
          {
            OpacitySelector._OneRadio.IsChecked = true;
          }
          // Two
          else if (Opacity < 0.55 && Opacity > 0.45)
          {
            OpacitySelector._TwoRadio.IsChecked = true;
          }
          // Five
          else if (Opacity < 1.05 && Opacity > 0.95)
          {
            OpacitySelector._FiveRadio.IsChecked = true;
          }
          else
          {
            OpacitySelector._OtherRadio.IsChecked = true;
          }

          OpacitySelector._suspendSet = false;
        }
      }
    }

  }
}